//
//  BasicStrategyMainViewController.m
//  blackjack
//
//  Created by kyocooro on 3/18/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "BasicStrategyMainViewController.h"

#define NUMBER_DEALER_CARDS		10
#define NUMBER_USER_CARDS		26
#define DECK_MODE_SINGLE	@"Single"
#define DECK_MODE_MULTI		@"Multiple"

@interface BasicStrategyMainViewController(Private)

int dealerPosition = -1;
int userPosition = -1;
BOOL isMultiDeck = FALSE;
UIView *mainView;

NSString *userValueArray[]	= {@"8",@"9",@"10",@"11",
							   @"12",@"13",@"14",@"15",
							   @"16",@"17-20",@"A,2",@"A,3",
							   @"A,4",@"A,5",@"A,6",@"A,7",
							   @"A,8",@"A,9",@"2,2",@"3,3",
							   @"4,4",@"5,5",@"6,6",@"7,7",
							   @"8,8",@"9,9",@"10,10",@"A,A"};
NSString *dealerValueArray[]= {@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"A"};
int multiTableValue[][10] = {
	{1, 1,  1,  1,  1,  1,  1,  1,  1,  1},
	{1, 3, 	3, 	3, 	3, 	1, 	1, 	1, 	1, 	1},
	{3, 3, 	3, 	3, 	3, 	3, 	3, 	3, 	1, 	1},
	{3, 3, 	3, 	3, 	3, 	3, 	3, 	3, 	3, 	1},
	{1, 1, 	0, 	0, 	0, 	1, 	1, 	1, 	1, 	1},
	{0, 0, 	0, 	0, 	0, 	1, 	1, 	1, 	1, 	1},
	{0, 0, 	0, 	0, 	0, 	1, 	1, 	1, 	1, 	1},
	{0, 0, 	0, 	0, 	0, 	1, 	1, 	1, 	1, 	1},
	{0, 0, 	0, 	0, 	0, 	1, 	1, 	1, 	1, 	1},
	{0, 0, 	0, 	0, 	0, 	0, 	0, 	0, 	0, 	0},
	{1, 1,  1,  3,  3,  1,  1,  1,  1,  1},
	{1, 1, 	1, 	3, 	3, 	1, 	1, 	1, 	1, 	1},
	{1, 1,  3, 	3, 	3, 	1, 	1, 	1, 	1, 	1},
	{1, 1, 	3, 	3, 	3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,2,2,2,2,2,1,1,1,1},
	{1,1,1,3,3,1,1,1,1,1},
	{3,3,3,3,3,3,3,3,1,1},
	{2,2,2,2,2,1,1,1,1,1},
	{2,2,2,2,2,2,1,1,1,1},
	{2,2,2,2,2,2,2,2,0,2},
	{2,2,2,2,2,0,2,2,0,0},
	{0,0,0,0,0,0,0,0,0,0},
	{2,2,2,2,2,2,2,2,2,2}};

- (NSString *)convertToValueString:(int)i;
- (void)viewResult;

@end


@implementation BasicStrategyMainViewController

@synthesize delegate;

- (IBAction)backButtonTouched {
	[delegate backToMainMenu];
}

- (IBAction)dealerButtonPressed:(id)sender{
	self.view = dealerNum;
	[dealerNum setUserInteractionEnabled:YES];
}

- (IBAction)userButtonPressed:(id)sender{
	self.view = userNum;
	[userNum setUserInteractionEnabled:YES];
}

- (IBAction)deckModeBtnTouched {
	NSString * title = [deckModeBtn titleForState:UIControlStateNormal];
	if([title isEqualToString:DECK_MODE_MULTI]) {
		[deckModeBtn setTitle:DECK_MODE_SINGLE forState:UIControlStateNormal];
		isMultiDeck = FALSE;
	}
	else {
		[deckModeBtn setTitle:DECK_MODE_MULTI forState:UIControlStateNormal];
		isMultiDeck = TRUE;
	}
	[self viewResult];
} 

- (void)chooseValueForDealer:(int)val {
	self.view = mainView;
	if(val < 0) return;
	[dealerLb setText:dealerValueArray[val]];
	dealerPosition = val;
	[self viewResult];
}

- (void)chooseValueForUser:(int)val {
	self.view = mainView;
	if(val	 < 0) return;
	[userLb setText:userValueArray[val]];
	userPosition = val;
	[self viewResult];
}

- (void)viewResult {
	if(dealerPosition >= 0 && dealerPosition < NUMBER_DEALER_CARDS
	   &&userPosition >= 0 && userPosition < NUMBER_USER_CARDS) {
		if(isMultiDeck) {
			[resultLb setText:[self convertToValueString:multiTableValue[userPosition][dealerPosition]]];
		}
		else {
			[resultLb setText:[self convertToValueString:singleTableValue[userPosition][dealerPosition]]];
		}
	}
	else {
		//[resultLb setText:@""];
	}
}

- (NSString *)convertToValueString:(int)i {
	switch (i) {
		case 0:
			return @"Stand";
			break;
		case 1:
			return @"Hit";
			break;
		case 2:
			return @"Split";
			break;
		case 3:
			return @"Double";
			break;
		default:
			break;
	}
	return @"";
}

- (void)viewDidLoad {
	[super viewDidLoad];
	CGRect frame = CGRectMake(0, 0, 320, 480);
	
	mainView = self.view;
	
	dealerNum	= [[DealerCardTable alloc]initWithFrame:frame];
	[dealerNum setDelegate:self];
	
	userNum		= [[UserCardTable alloc] initWithFrame:frame];
	[userNum setDelegate:self];
}

- (void)dealloc {
    [super dealloc];
}


@end

